﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Dynamic;
using System.Reflection;

namespace MoneyBook.Assets.Resources {
    public abstract class ResourceWrapper<T> : DynamicObject, ICustomTypeProvider {

        private T target;

        protected ResourceWrapper(T target) {
            this.target = target;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            PropertyInfo property = typeof(T).GetProperty(binder.Name);
            if (property == null || !property.CanRead) {
                result = null;
                return false;
            }
            result = property.GetValue(this.target, null);
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value) {
            PropertyInfo property = typeof(T).GetProperty(binder.Name);
            if (property == null || !property.CanWrite) {
                return false;
            }
            property.SetValue(this.target, value, null);
            return true;
        }

        public Type GetCustomType() {
            return typeof(T);
        }
    }
}
